package com.jz.mx.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jz.mx.bean.Aphereses;
import com.jz.mx.bean.Sfproviderreg;
import com.jz.mx.mapper.sqlServer.ApheresesMapper;
import com.jz.mx.mapper.sqlServer.SfproviderregMapper;
import com.jz.mx.service.SfproviderregService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jz.netthings.application.providerCollection.bean.ProviderCollection;
import com.jz.netthings.application.providerCollection.mapper.mysql.ProviderCollectionMapper;
import com.jz.netthings.application.providerReg.bean.ProviderReg;
import com.jz.netthings.application.providerReg.mapper.mysql.ProviderRegMapper;
import com.jz.netthings.exception.AdscmSystemException;
import com.jz.netthings.utils.AdscmBaseUtils;
import com.jz.netthings.utils.AdscmDateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yyy
 * @since 2019-08-19
 */
@Service
public class SfproviderregServiceImpl extends ServiceImpl<SfproviderregMapper, Sfproviderreg> implements SfproviderregService {

    @Autowired
    SfproviderregMapper sfproviderregMapper;
    @Autowired
    ProviderRegMapper providerRegMapper;
    @Autowired
    ApheresesMapper apheresesMapper;
    @Autowired
    ProviderCollectionMapper providerCollectionMapper;

    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void syncRegMsg() throws AdscmSystemException {
        logger.info("同步登记信息");
        long startTime = System.currentTimeMillis();
        Wrapper<ProviderReg> pregWrapper = new EntityWrapper<>();
        pregWrapper.ge("regTime","2017-08-17");
//        pregWrapper.le("regTime","2017-07-01");
        List<ProviderReg> regList = providerRegMapper.selectList(pregWrapper);
        if (AdscmBaseUtils.getInstance().listIsNotEmpty(regList)){
            for (ProviderReg reg : regList) {
                List<Sfproviderreg> mxRegList = sfproviderregMapper.selectByRegNo(reg.getRegisterNo());
                if (AdscmBaseUtils.getInstance().listIsNotEmpty(mxRegList)){
                    Sfproviderreg mxReg = mxRegList.get(0);
                    if(!AdscmBaseUtils.getInstance().isNotEmpty(reg.getIdNo(),null)){
                        reg.setIdNo(mxReg.getIdno());
                    }
                    reg.setRegTime(mxReg.getRegtime());
                    providerRegMapper.updateById(reg);
                }
            }
        }

        logger.info("同步耗时：【"+((System.currentTimeMillis()) - startTime) +"】毫秒");

    }
    @Override
    public void syncCollMsg() throws AdscmSystemException {
        logger.info("同步采浆信息");
        long startTime = System.currentTimeMillis();
        Wrapper<ProviderCollection> collWrapper = new EntityWrapper<>();
        collWrapper.ge("registerNo","201805190001");
        collWrapper.le("registerNo","201805191001");
//        pregWrapper.le("regTime","2017-07-01");
        List<ProviderCollection> collList = providerCollectionMapper.selectList(collWrapper);
        if (AdscmBaseUtils.getInstance().listIsNotEmpty(collList)){
            for (ProviderCollection coll : collList) {
                Date collDate = coll.getCollectionDate();
                if (collDate != null){
                    String b = AdscmDateUtils.getDateFirstSeconds(collDate);
                    String e = AdscmDateUtils.getDateLastSeconds(collDate);
                    List<Aphereses> apheresesList = apheresesMapper.getByIdNoAndTime(coll.getIdNo(), b, e);
                    if (AdscmBaseUtils.getInstance().listIsNotEmpty(apheresesList)){
                        Aphereses ap = apheresesList.get(0);
                        coll.setInstallStartTime(ap.getInstallstarttime());
                        coll.setInstallEndTime(ap.getInstallendtime());
                        if (coll.getInstallEndTime() != null && coll.getInstallStartTime() != null){
                            Long between = coll.getInstallEndTime().getTime() - coll.getInstallStartTime().getTime();
                            coll.setInstallTaken(between);
                        }
                        if (coll.getInstallTaken() != null){
                            Integer min = Integer.valueOf(coll.getInstallTaken()+"");
                            coll.setInstallTakenName(AdscmDateUtils.getInputSecondsOuptMin(min));
                        }
                        coll.setPunctureStartTime(ap.getPuncturestarttime());
                        coll.setPunctureEndTime(ap.getPunctureendtime());
                        coll.setPunctureTaken(ap.getPuncturetaken());

                        coll.setStartTime(ap.getPunctureendtime());
                        coll.setEndTime(ap.getEndTime());
                        if (coll.getEndTime() != null && coll.getStartTime() != null){
                            Long between = coll.getEndTime().getTime() - coll.getStartTime().getTime();
                            coll.setCollTaken(between);
                        }
                        if (coll.getCollTaken() != null){
                            Integer min = Integer.valueOf(coll.getCollTaken()+"");
                            coll.setCollTakenName(AdscmDateUtils.getInputSecondsOuptMin(min));
                        }
                        coll.setUninstallStartTime(ap.getUninstallstarttime());
                        coll.setUninstallEndTime(ap.getUninstallendtime());
                        if (coll.getUninstallEndTime() != null && coll.getUninstallStartTime() != null){
                            Long between = coll.getUninstallEndTime().getTime() - coll.getUninstallStartTime().getTime();
                            coll.setUninstallTaken(between);
                        }
                        if (coll.getUninstallTaken() != null){
                            Integer min = Integer.valueOf(coll.getUninstallTaken()+"");
                            coll.setUninstallTakenName(AdscmDateUtils.getInputSecondsOuptMin(min));
                        }

                        providerCollectionMapper.updateById(coll);
                    }

                }
            }
        }

        logger.info("同步耗时：【"+((System.currentTimeMillis()) - startTime) +"】毫秒");

    }
}
